package com.example.android.commitcontent.ime.keyboard.managers;

import com.example.android.commitcontent.ime.beans.PinYinCharacter;
import com.example.android.commitcontent.ime.dict.libs.PinYinCombinationDict;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 拼音组合工具类
 */
public class PinYinCombination {

    private static final PinYinCombination INSTANCE = new PinYinCombination();

    private PinYinCombinationListener mPinYinCombinationListener;
    private List<PinYinCharacter> mPinYinCharacterList = new ArrayList<>();

    private PinYinCombination() {

    }

    public static PinYinCombination instance() {
        return INSTANCE;
    }

    /**
     * 向拼音组合里添加字母
     *
     * @param pinYinCharacter
     */
    public void addPinYinCharacter(PinYinCharacter pinYinCharacter) {
        //如果第一个是分隔符将不添加
        if (!(mPinYinCharacterList.size() == 0 && pinYinCharacter.isSymbol()))
            //如果输入的是分隔符&列表最后是分隔符将不被添加
            if (!(pinYinCharacter.isSymbol() && mPinYinCharacterList.size() > 0 && mPinYinCharacterList.get(mPinYinCharacterList.size() - 1).isSymbol()))
                mPinYinCharacterList.add(pinYinCharacter);
        generateCombinationList();
    }

    /**
     * 生成拼音推荐列表
     */
    private void generateCombinationList() {
        List<String> pinyinList = new ArrayList<>();
        List<String> pinyinSuggestionList = new ArrayList<>();
        String candidateText = "";
        if (mPinYinCharacterList.size() > 0) {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < mPinYinCharacterList.size(); i++) {
                PinYinCharacter character = mPinYinCharacterList.get(i);
                candidateText += character.text;
                if (character.type == PinYinCharacter.Type.SplitSymbol) {
                    pinyinList.add(builder.toString());
                    builder.delete(0, builder.length());
                    continue;
                }
                builder.append(character.text);
            }
            pinyinList.add(builder.toString());
            String firstPinYinString = getFirstPinYinString();
            char ch = firstPinYinString.charAt(0);
            if (ch >= 97 && ch <= 122) {
                int index = ch - 97;
                for (String s : PinYinCombinationDict.pinyinAllList.get(index)) {
                    if (s.startsWith(firstPinYinString))
                        pinyinSuggestionList.add(s);
                }
            }
        }
        mPinYinCombinationListener.onPinYinCombinationGenerated(pinyinList, candidateText, pinyinSuggestionList);
        if (pinyinList.size() == 0)
            mPinYinCombinationListener.onCombinationCleared();
    }

    /**
     * 获取第一个拼音组合的字符串
     *
     * @return
     */
    private String getFirstPinYinString() {
        StringBuilder builder = new StringBuilder("");
        for (PinYinCharacter pinYinCharacter : mPinYinCharacterList) {
            if (pinYinCharacter.isSymbol())
                break;
            builder.append(pinYinCharacter.text);
        }
        return builder.toString();
    }

    /**
     * 是否能回退
     *
     * @return
     */
    public boolean canBackspace() {
        return mPinYinCharacterList.size() > 0;
    }

    /**
     * 退格一个字母
     */
    public void backspace() {
        mPinYinCharacterList.remove(mPinYinCharacterList.size() - 1);
        generateCombinationList();
    }

    /**
     * 注册监听
     *
     * @param pinYinCombinationListener
     */
    public void register(PinYinCombinationListener pinYinCombinationListener) {
        mPinYinCombinationListener = pinYinCombinationListener;
    }

    /**
     * 清除所有字母
     */
    public void clear() {
        if (mPinYinCharacterList.size() == 0) return;
        mPinYinCharacterList.clear();
        generateCombinationList();
    }

    /**
     * 移除第一个拼音组合字母，如 ni,hao 移除后变成 hao
     */
    public void removeFirstPinYin() {
        Iterator<PinYinCharacter> iterator = mPinYinCharacterList.iterator();
        while (iterator.hasNext()) {
            PinYinCharacter character = iterator.next();
            boolean flag = false;
            if (character.type == PinYinCharacter.Type.SplitSymbol)
                flag = true;
            iterator.remove();
            if (flag)
                break;
        }
        generateCombinationList();
    }

    /**
     * 用新的拼音字符替换第一个拼音组合
     *
     * @param pinyin
     */
    public void replaceFirstPinYin(String pinyin) {
        Iterator<PinYinCharacter> iterator = mPinYinCharacterList.iterator();
        while (iterator.hasNext()) {
            PinYinCharacter next = iterator.next();
            if (next.isSymbol())
                break;
            else
                iterator.remove();
        }
        mPinYinCharacterList.add(0, new PinYinCharacter(pinyin, PinYinCharacter.Type.Pinyin));
        generateCombinationList();
    }

    public interface PinYinCombinationListener {
        void onPinYinCombinationGenerated(List<String> combinationList, String candidateText, List<String> pinyinSuggestionList);

        void onCombinationCleared();
    }
}
